package restful.jersey.resource;

import java.util.ArrayList;
import java.util.List;

import javax.ws.rs.DefaultValue;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;

import restful.domain.SimplePOJO;
import restful.domain.SimplePOJOList;

import com.sun.jersey.api.NotFoundException;

// This is root resource
@Path("/hello")
public class JerseyService implements IJerseyService {
	  @GET // The HTTP method by which we access the service 
	  @Produces(MediaType.TEXT_HTML) // The output MIME Type 
	  public String getMethod() 
	  { 
	     return "<b>Hello World, Welcome to Jersey + Spring Integrattion</b>"; 
	  }
	  
	  @Path("{name}/{age}")
	  @GET
	  @Produces(MediaType.TEXT_PLAIN)
	  public String addString(@PathParam("name") String name, @PathParam("age") int age) {
		  throw new NotFoundException(name + " is not found " + age);
	  }
	  
	  @Path("/query")
	  @GET
	  @Produces(MediaType.TEXT_PLAIN)
	  public String getByQueryParam(@DefaultValue("default") @QueryParam("name") String name,
			  						@DefaultValue("99") @QueryParam("age") int age) {
		  return "getByQueryParam : name " + name + " age " + age;
	  }
	  
	  @Path("/form")
	  @POST
	  @Produces(MediaType.TEXT_PLAIN)
	  public String getByForm(@FormParam("name") String name, 
			  				  @FormParam("age") int age) {
		  return "Form request : name-" + name + " age " + age;
	  }
	  
	  @GET
	  @Path("/json/simple")
	  @Produces(MediaType.APPLICATION_JSON)
	  public SimplePOJO getSimpleJSON() {
		  return new SimplePOJO("SimpleJSON", 999);
	  }
	  
	  @GET
	  @Path("/json/list")
	  @Produces(MediaType.APPLICATION_JSON)
	  public SimplePOJOList getListJSON() {
		  List<SimplePOJO> lists = new ArrayList<SimplePOJO>();
		  for (int i = 0; i < 1; i++) {
			  SimplePOJO pojo = new SimplePOJO("name" + i, 100 + i);
			  lists.add(pojo);
		  }
		  SimplePOJOList pojoList = new SimplePOJOList();
		  pojoList.setLists(lists);
		  return pojoList;
	  }
}
